<?php

/* 
 * Copyright (C) 2007 Revodes <http://www.revodes.com/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once("config.php");
require_once("libs/Smarty.class.php");
require_once("database.php");
require_once("session.php");
require_once("torrent.php");
		
class Core {
	
	var $node;
	var $smarty;
	var $database;
	var $config;
	var $session;
	var $torrent;
	
	function Core() {
		$this->config = new Config();
		$this->database = new Database($this);
		$this->session = new Session($this);
		$this->torrent = new Torrent($this);
		
		if(!class_exists("Smarty")) $this->error("Smarty class missing.");
		if(!class_exists("Database")) $this->error("Database class missing.");
		
		$this->smarty = new Smarty();
		
		$this->smarty->template_dir = $this->config->config["directory"] . "templates/" . $this->config->config["template"];
		$this->smarty->compile_dir = $this->config->config["directory"] . 'templates_c';
		$this->smarty->cache_dir = $this->config->config["directory"] . 'cache';
		$this->smarty->config_dir = $this->config->config["directory"] . 'configs';
		
		$this->smarty->assign_by_ref("core", $this);
		
		if ($this->config->config["edited"] == false) {
			$this->setup();
			exit();
		}
		
		if ($this->database->connected == false) {
			$this->error($this->database->error, "MySQL Error");
		}
	}
	
	function error($msg = "Undefined error", $title = "Error") {
		die($msg);
	}
	
	// @info: safe get vars/user contributed content
	function strip($var) {
		return htmlspecialchars(stripcslashes($var));
	}
	
	// @info: initiate node and start smarty template
	function node($redirect = false) {
		global $config;
		
		if (is_string($redirect)){
			$url = './?n=' . $redirect;
			header('Location: ' . $url);
			ob_end_flush();
			flush();
			exit();
		}
		
		$this->node = $this->strip($_GET["n"]);
		$this->node = !empty($this->node)? $this->node : 'news';
		$node = explode('/', $this->node);
		
		$file = 'templates/' . $this->config->config['template'].'/node_'.$node[0].'.tpl';
		if (is_file($file))
			$this->smarty->display('node_'.$node[0].'.tpl');
		else {
			$this->error("No such template " . $this->config->config['template'] . "/" . $node[0]);
		}
		return;
	}
	
	function nodePartOf($node){
		$res = array_diff_assoc(explode('/', $node), explode('/', $this->node));
		if (empty($res)) return true;
		return false;
	}
	
	function setup() {
		die("Unedited config!");
	}
	
	function makePercent($amount, $total) {
		$percent = round($amount * 100 / $total);
		return $percent;
	}
	
}

$core = new Core();

?>